from os import path,environ,listdir
import regex,sys,string
_modules=[]
patt=regex.compile("^\(Init[A-Za-z_0-9]+\)\.py$")

def CollectFiles(dir,l):
	try:
		files=listdir(dir)
	except:
		return
	for file in files:
		if patt.match(file)>0:
			mod=patt.group(1)
			if not mod in l:
				l.append(mod)
def InitNew():
	l=[]
	for p in sys.path:
		if string.find(p,"editor") >0 and string.find(p,"config")>0:
			CollectFiles(p,l)
	l.sort()
	for m in l:
		try:
			_modules.append(__import__(m))
		except:
			# catch any errors when importing the module
			import traceback
			traceback.print_exc()
def ReInit(force=0):
	if not force:
		return
	if not  _modules:
		return
	import RegisterImpl
	RegisterImpl.Reset()
	for m in _modules:
		try:
			reload(m)
		except:
			# catch any errors when importing the module
			import traceback
			traceback.print_exc()
def Init():
	if _modules:
		ReInit()
	else:
		InitNew()
Init()
